package com.example.customview.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.example.customview.R;

public class InputNumberView extends RelativeLayout {
    private static final String TAG = "InputNumberView";
    private int mCurrentNum = 0;
    private View mMinusBtn = null;
    private View mPlusBtn = null;
    private EditText mValueEdt = null;
    private OnNumberValueChangeListener mOnNumberValueChangeListener = null;
    private int mMax;
    private int mMin;
    private int mStep;
    private boolean mDisable;
    private int mBtnBgRes;
    private int mDefaultValue;

    public InputNumberView(Context context) {
        this(context,null);  // 确保同一个入口
    }

    public InputNumberView(Context context, AttributeSet attrs) {
        this(context, attrs,0);  // 确保同一个入口
    }

    public InputNumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取相关属性
        initAttrs(context, attrs);

        initView(context);
        // 设置事件
        setupEven();
    }

    public View getMinusBtn() {
        return mMinusBtn;
    }

    public void setMinusBtn(View minusBtn) {
        mMinusBtn = minusBtn;
    }

    public int getMax() {
        return mMax;
    }

    public void setMax(int max) {
        mMax = max;
    }

    public int getMin() {
        return mMin;
    }

    public void setMin(int min) {
        mMin = min;
    }

    public int getStep() {
        return mStep;
    }

    public void setStep(int step) {
        mStep = step;
    }

    public boolean isDisable() {
        return mDisable;
    }

    public void setDisable(boolean disable) {
        mDisable = disable;
    }

    public int getBtnBgRes() {
        return mBtnBgRes;
    }

    public void setBtnBgRes(int btnBgRes) {
        mBtnBgRes = btnBgRes;
    }

    public int getDefaultValue() {
        return mDefaultValue;
    }

    public void setDefaultValue(int defaultValue) {
        mDefaultValue = defaultValue;
        this.mCurrentNum = defaultValue;
        this.upDateText();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        // 获取相关属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.InputNumberView);
        mMax = typedArray.getInt(R.styleable.InputNumberView_max,0);
        mMin = typedArray.getInt(R.styleable.InputNumberView_min,0);
        mStep = typedArray.getInt(R.styleable.InputNumberView_step,1);
        mDisable = typedArray.getBoolean(R.styleable.InputNumberView_disable,true);
        mBtnBgRes = typedArray.getResourceId(R.styleable.InputNumberView_btnBackground,-1);
        mDefaultValue = typedArray.getInt(R.styleable.InputNumberView_defaultValue,0);
        this.mCurrentNum = mDefaultValue;
        typedArray.recycle();
    }

    private void setupEven() {
        mMinusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCurrentNum+mStep < mMin) {
                    Log.d(TAG,"就要小于最小值了");
                    return;
                }
                mCurrentNum = mCurrentNum - mStep;
                upDateText();
            }
        });

        mPlusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCurrentNum+mStep > mMax) {
                    Log.d(TAG,"就要超过最大值了");
                    return;
                }
                mCurrentNum = mCurrentNum + mStep;
                upDateText();
                //
            }
        });
    }

    private void initView(Context context) {
        // 把其他的子view 载进来
        // 所有的view 都有context
        // 以下代码等价
        // LayoutInflater.from(context).inflate(R.layout.input_number_view,this,true);
        //LayoutInflater.from(context).inflate(R.layout.input_number_view,this);
        View view = LayoutInflater.from(context).inflate(R.layout.input_number_view,this,false);
        addView(view);
        mMinusBtn = view.findViewById(R.id.minus_btn);
        mPlusBtn = view.findViewById(R.id.plus_btn);
        mValueEdt = view.findViewById(R.id.value_edt);
        // 初始化控件值
        upDateText();
        mMinusBtn.setEnabled(!mDisable);
        mPlusBtn.setEnabled(!mDisable);
    }

    public int getNum() {
        return mCurrentNum;
    }

    public void setNum(int value) {
        mCurrentNum = value;
        this.upDateText();

    }

    /*
    * 更新数值
    * */

    private void upDateText() {
        mValueEdt.setText(String.valueOf(mCurrentNum));
        if (mOnNumberValueChangeListener != null) {
            mOnNumberValueChangeListener.onNumberChange(this.mCurrentNum);
        }
    }

    public void setOnNumberValueChangeListener(OnNumberValueChangeListener listener) {
        this.mOnNumberValueChangeListener = listener;
    }

   public interface OnNumberValueChangeListener {
        void onNumberChange(int value);
    }
}
